<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi;

/** 
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class ProductController extends AdminController {

    /**
     * 产品 
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
  


        public function product(){
         $map["id"]         =  (int)$_POST['id'];
         $map["number"]     = $_POST['number'];
         $map["name"] = $_POST['name'];
         $map["specification"]    = $_POST['specification'];
         $map["category"]        = $_POST['category'];
         $map["remark"]       = $_POST['remark'];  
        if(IS_POST){   
            $res = M("product")->where('id='. $map["id"])->save($map);  
        }  
           $info = M()
            ->table('one_product')
            ->where('is_delete=0')  
            ->select();
         // print_r($info);die;   
        foreach ($info as $key => $value) {
            $info[$key]['create_time']=date('Y-m-d',$value['create_time']); 
        }
        $this->assign('info',$info);
        $this->display();  
    }
    
    
    // 编辑
    public function compile()
    {
        $map['id'] = $_POST['ID'];  
        $map['is_delete'] = 0; 
        $list = M("product")->where($map)->select(); 
        $this->ajaxReturn($list); 
    }
    // 删除
    public function deletes()
    {
        // $map['id'] = $_GET['id'];  
        $id = I('id');  
        $data['is_delete']=1; 
        $re = M('product')->where(array('id'=>$id))->save($data);
        
        if($re == true){
            $this->success('删除成功');
        }
        else{
            $this->error('删除失败');
        } 
    }
    // 禁用
    public function forbidden()
    {  
        $id = I('id');  
        $data['status']=0; 
        $re = M('product')->where(array('id'=>$id))->save($data);
        
        if($re == true){
            $this->success('禁用成功');
        }
        else{
            $this->error('禁用失败');
        } 
    } 
    // 启用start
    public function start()
    {  
        $id = I('id');  
        $data['status']=1; 
        $re = M('product')->where(array('id'=>$id))->save($data);
        
        if($re == true){
            $this->success('启用成功');
        }
        else{
            $this->error('启用失败');
        } 
    } 
    // 新增产品
     public function GetRandStr($len)   
    {   
        $chars = array(   
             "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"   
        );   
        $charsLen = count($chars) - 1;   
        shuffle($chars);     
        $output = "";   
        for ($i=0; $i<$len; $i++)   
        {   
            $output .= $chars[mt_rand(0, $charsLen)];   
        }    
        return $output;    
    } 
    public function product_add(){ 

         $arr=$this->GetRandStr(4);  
         $data['number'] =   'CP'.date('YmdHis' ,time()).$arr;  //订单编号
         $data['name'] = $_POST['name']; 
         $data['specification'] = $_POST['specification']; 
         $data['category'] = $_POST['category']; 
         $data['remark'] = $_POST['remark'];   
         $data['create_time'] = time();  
         $res = M("product")->add($data);   
        if($res == true){
            $this->success('新增成功');
        }
        else{
            $this->error('新增失败');
        }   
    }
    // 产品分类
     public function product_type(){  
           $info = M()
            ->table('one_product_type') 
            ->where('is_delete=0')
            ->select();  
        foreach ($info as $key => $value) {
            $info[$key]['create_time']=date('Y-m-d',$value['create_time']); 
        }
        $this->assign('info',$info);
        $this->display();  
    }

    //新增分类 type_add
   public function type_add(){  
         $data['name'] = $_POST['name'];  ;   
         $data['create_time'] = time();  
         $res = M("product_type")->add($data);   
        if($res == true){
            $this->success('新增成功');
        }
        else{
            $this->error('新增失败');
        }   
    }
        // 分类删除
    public function type_del()
    { 
        $id = I('id');  
        $data['is_delete']=1; 
        $re = M('product_type')->where(array('id'=>$id))->save($data); 
        if($re == true){
            $this->success('删除成功');
        }
        else{
            $this->error('删除失败');
        } 
    }


}
